﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class Optimization
{
    static public void Proc(ref string Code)//メインプロセス
    {
        Optimization.EndSign(ref Code);//]　を　];　に変更する
        Optimization.Semicolon(ref Code);// ; の一つ前にスペースを挿入する
        Optimization.Colon(ref Code);// : の前後にスペースを挿入する

        Optimization.DoubleDot(ref Code);//ダブルドット処理
        Optimization.Dot(ref Code);//ドット処理

        Optimization.PlusPlus(ref Code);//++処理
        Optimization.Plus(ref Code);//+処理
        Optimization.PlusEqual(ref Code);//+=処理
        Optimization.MinusMinus(ref Code);//--処理

        Optimization.Minus(ref Code);//-処理
        Optimization.MinusEqual(ref Code);//-=処理

        Optimization.DivideEqual(ref Code);// /=処理

        Optimization.MultiplyEqual(ref Code);// *= 処理

        Optimization.PercentEqual(ref Code);// %= 処理

        Optimization.Variable(ref Code); // 変数の初期化関連の処理

        Optimization.Parentheses(ref Code); // ( ) の処理
    }

    static public void Semicolon(ref string Code) // ;
    {
        Code = Regex.Replace(Code, ";", " ; ");
    }

    static public void EndSign(ref string Code) // ]
    {
        Code = Regex.Replace(Code, "]", "]; ");
    }

    static public void Colon(ref string Code)// :
    {
        Code = Regex.Replace(Code, ":", " : ");
    }

    static public void DoubleDot(ref string Code)// ..
    {
      Code = Regex.Replace(Code, @"\.\.", " .. ");
    }

    static public void Dot(ref string Code)// .
    {
        Regex r = new Regex(@"(?<N>[^\.])\.(?<B>[^\.])");
        Code = r.Replace(Code, "${N} . ${B}");
    }

    static public void PlusPlus(ref string Code)// ++
    {
        Regex r = new Regex(@"\+\+");
        Code = r.Replace(Code, "+=1");
    }

    static public void Plus(ref string Code)// +
    {
        Regex r = new Regex(@"(?<A>[^+])\+(?<B>[^=])");
        Code = r.Replace(Code, "${A} + ${B}");
    }
    static public void PlusEqual(ref string Code)// +=
    {
        Regex r = new Regex(@"\+=");
        Code = r.Replace(Code, " += ");

        r = new Regex(@"(?<Name>[ \w]+)\s\+=\s(?<Names>.+?)");//x = x + y
        Match m = r.Match(Code);

        #region debug
#if DEBUG
        Console.WriteLine("[Optimization::PlusEqual::Left]{0}", m.Groups["Name"].Value);
        Console.WriteLine("[Optimization::PlusEqual::Right]{0}", m.Groups["Names"].Value);
#endif
        #endregion
        Code = r.Replace(Code, @" ${Name} = ${Name} + ${Names} ");

    }

    static public void MinusMinus(ref string Code)// --
    {
        Regex r = new Regex(@"\-\-");//--
        Code = r.Replace(Code, "-=1");
    }

    static public void Minus(ref string Code) //-
    {
        Regex r = new Regex(@"(?<A>[^+])\-(?<B>[^=])");//xx-xx
        Code = r.Replace(Code, "${A} - ${B}");
    }
    static public void MinusEqual(ref string Code)// -=
    {
        Regex r = new Regex(@"\-=");
        Code = r.Replace(Code, " -= ");

        r = new Regex(@"(?<Name>[ \w]+)\s\-=\s(?<Names>.+?)");//x = x - y
        Code = r.Replace(Code, @" ${Name} = ${Name} - ${Names} ");
    }
    static public void MultiplyEqual(ref string Code)// *=
    {
        Regex r = new Regex(@"\*=");
        Code = r.Replace(Code, " *= ");

        r = new Regex(@"(?<Name>[ \w]+)\s\*=\s(?<Names>.+?)");//x = x * y
        Code = r.Replace(Code, @" ${Name} = ${Name} * ${Names} ");
    }

    static public void DivideEqual(ref string Code)// /=
    {
        Regex r = new Regex(@"\/=");
       Code = r.Replace(Code, " /= ");
        r = new Regex(@"(?<Name>[ \w]+)\s\/=\s(?<Names>.+?)");//x = x / y
        Code = r.Replace(Code, @" ${Name} = ${Name} / ${Names} ");

    }

    static public void PercentEqual(ref string Code)//%=
    {
        Regex r = new Regex(@"\%=");
        Code = r.Replace(Code, " %= ");

        r = new Regex(@"(?<Name>[ \w]+)\s\%=\s(?<Names>.+?)");//x = x / y

        Code = r.Replace(Code, @" ${Name} = ${Name} % ${Names} ");

    }

    static public void Parentheses(ref string Code)//( )
    {
        Regex r = new Regex(@"\(");
        Code = r.Replace(Code, " ( ");
        r = new Regex(@"\)");
        Code = r.Replace(Code, " ) ");
    }

    static public void Variable(ref string Code)// var xx = 0;
    {
        Regex r = new Regex(@"var\s+(?<Name>\w+)\s+=\s(?<Value>[\w\d]+)");

        Code = r.Replace(Code, @"(var ${Name} ; ${Name} = ${Value} )");
    }
}
